﻿@*
	Iván Loire - www.iloire.com
	Please readme README file for license terms.

	ASP.NET MVC3 ACME Invocing app (demo app for training purposes)
*@

@using MvcPaging;
@model IPagedList<Customer>

@{
    ViewBag.Title = "Customers";
}

<div id="ajaxResponse">

<h1>List <small>of Customers</small></h1>

<script>

    $(document).ready(function () {

        function clearErrors() {
            $('#msgErrorNewCustomer').html('');
            $('#msgError').html('');
        }

        function writeError(control, msg) {
            var err_msg = '<div class="alert-message error"><a class="close" href="#">×</a><p>' + msg + '</p></div>';
            $('#' + control).html(err_msg);
        }

        function getRequest(url) {
            $.ajax({
                url: url,
                context: document.body,
                success: function (data) {
                    $('.modal-body p.body').html(data);
                    $(this).addClass("done");
                    $('#my-modal').modal('show');
                },
                error: function (err) {
                    writeError('msgErrorNewInvoiceDetails', err);
                }
            });
        }

        $('.closeModal').live('click', function () {
            $('#my-modal').modal('hide');
        });

        $('#my-modal form').live('submit', function () {
            clearErrors();

            $.post($(this).attr('action'), $(this).serialize(), function (data, status) {
                $('#my-modal').modal('hide');
                $("#customerList").html(data);

            }).error(function (error, status) {
                writeError('msgError', 'Error processing request. Please check errors and try again!');
                $('.modal-body p.body').html(error.responseText);

            });
            return false;
        });



        $('.editCustomer').live('click', function () {
            clearErrors();

            var id = $(this).attr("idCustomer");
            var url = '@Url.Content("~/Customer/Edit")/' + id;

            getRequest(url);

            return false;
        });


        $('a.newCustomer').live('click', function () {
            clearErrors();
            var url = '@Url.Content("~/Customer/Create")';

            getRequest(url);

            return false;
        });
    });
</script>
<p>
    @Html.ActionLink("[+] Create new Customer", "Create", null, new { @class = "btn newCustomer primary" })
</p>

@{Html.RenderPartial("SearchBoxCompany", "Customer");}


<div id="msgErrorNewCustomer"></div>
     <div id="my-modal" class="modal hide fade" style="width:850px;margin:-40% 0 0 -30%;">
            <div class="modal-header">
                <a href="#" class="close">&times;</a>
                <h3>Customer edition</h3>
            </div>
            <div class="modal-body">
                <div id="msgError"></div>
                <p class="body">
                   
                </p>
            </div>
            <div class="modal-footer">
             
            </div>
    </div>


    <div id="customerList">
        @{Html.RenderPartial("CustomerListPartial", Model);}
    </div>
</div>